﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace echuCMS
{
    public partial class FormDistributeCustomerToTheSalesMan : Form
    {
        private CustomerMain customerMain;
        private BindingSource bsSalesMan = new BindingSource();
        public FormDistributeCustomerToTheSalesMan()
        {
            InitializeComponent();
        }
        public FormDistributeCustomerToTheSalesMan(CustomerMain customerMain)
        {
            this.customerMain = customerMain;
            InitializeComponent();
        }

        private void FormDistributeCustomerToTheSalesMan_Load(object sender, EventArgs e)
        {
            dataGridViewSalesMan.AutoGenerateColumns = false;
            if (customerMain != null)
            {
                textBoxCustomerCode.Text = customerMain.CustomerCode;
                textBoxCustomerName.Text = customerMain.CustomerName;
                textBoxTrades.Text = customerMain.CustomerTrades;
                textBoxCustomerType.Text = customerMain.CustomerType;
                textBoxCustomerLevel.Text = customerMain.CustomerLevel;
                textBoxCustomerOrigin.Text = customerMain.CustomerOrigin;
            }
            //查询所有已经存在的业务员
            getAllSalesMan();
        }
        private void getAllSalesMan()
        {
            bsSalesMan.DataSource = SalesManService.getAllSalesMan();
            dataGridViewSalesMan.DataSource = bsSalesMan;
        }
        private void dataGridViewSalesMan_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && dataGridViewSalesMan.Columns[e.ColumnIndex].Name == "C选择")
            {
                for (int i = 0; i < this.dataGridViewSalesMan.Rows.Count; i++)
                {
                    this.dataGridViewSalesMan.Rows[i].Cells["C选择"].Value = false;
                }
                this.dataGridViewSalesMan.Rows[e.RowIndex].Cells["C选择"].Value = true;
            }
        }
        /// <summary>
        /// 确定分配
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            int s = 0;
            //检查有没有选择业务员
            for (int i = 0; i < this.dataGridViewSalesMan.Rows.Count; i++)
            {
                if (this.dataGridViewSalesMan.Rows[i].Cells["C选择"].Value != null && bool.Parse(this.dataGridViewSalesMan.Rows[i].Cells["C选择"].Value.ToString()) == true)
                {
                s++;
                }
            }

            if (s > 0)
            {
                //公共池客户转换为潜在客户
                List<CustomerPotential> customerPotentialList = new List<CustomerPotential>();
                //潜在客户——业务员中间表
                List<PotentialSalesman> potentialSalesmanList = new List<PotentialSalesman>();
                //将公共池客户转为潜在客户表
                CustomerPotential customerPotential = new CustomerPotential();
                customerPotential.Id = Guid.NewGuid().ToString();
                customerPotential.CustomerMainId = customerMain.Id;
                customerPotential.Status = 1;
                customerPotential.CreateTime = DateTime.Now;
                customerPotentialList.Add(customerPotential);
                //将潜在客户id和业务员id插入中间表
                SalesMan salesMan = bsSalesMan.Current as SalesMan;
                PotentialSalesman potentialSalesman = new PotentialSalesman();
                potentialSalesman.Id = Guid.NewGuid();
                potentialSalesman.PotentialId = customerPotential.Id;
                potentialSalesman.SalesManId = salesMan.Id;
                potentialSalesman.Status = 1;
                potentialSalesman.CreateTime = DateTime.Now;
                potentialSalesmanList.Add(potentialSalesman);
                //执行将公共池客户转到潜在客户
                int i = CustomerMainService.transferCustomerMainToPotential(customerPotentialList, potentialSalesmanList);
                if (i > 0)
                {
                    MessageBox.Show("分配成功");
                    button1.Enabled = false;
                }
                else
                {
                    MessageBox.Show("分配失败，请联系开发者");
                }
            }
            else
            {
                MessageBox.Show("请选择需要分配的业务员");
            }
        }
    }
}
